package com.demo.多线程;

/**
 * @Program: java
 * @ClassName UnSafeTicket
 * @Author: liutao
 * @Description: 并发问题
 * @Create: 2023-10-27 15:36
 * @Version 1.0
 **/

public class UnSafeTicket implements Runnable{
    private int ticketNum = 10;

    public static void main(String[] args) {
        UnSafeTicket ticket = new UnSafeTicket();
        new Thread(ticket,"小明").start();
        new Thread(ticket,"小红").start();
        new Thread(ticket,"李白").start();
    }

    @Override
    public void run() {
        while (true) {
            if (ticketNum <= 0) {
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "抢到了第"+ticketNum--+"票");
        }
    }
}
